<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="输入种类" prop="typeName">
      <el-input v-model="dataForm.typeName" placeholder="输入种类名称"></el-input>
    </el-form-item>
    <el-form-item label="是否显示" prop="showStatus">
      <el-switch
        v-model="dataForm.showStatus"
        active-color="#13ce66"
        inactive-color="#ff4949"
        :active-value="1"
        :inactive-value="0">
      </el-switch>
      <!-- <el-input v-model="dataForm.showStatus" placeholder="是否显示[0-否,1是]"></el-input> -->
    </el-form-item>
    <el-form-item label="排序" prop="sort">
      <el-input-number v-model="dataForm.sort" controls-position="right" :min="0" :max="10"></el-input-number>
      <!-- <el-input v-model="dataForm.sort" placeholder="排序"></el-input> -->
    </el-form-item>
    <el-form-item label="图标地址" prop="icon">
      <el-input v-model="dataForm.icon" placeholder="图标地址"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          typeId: 0,
          typeName: '',
          showStatus: '',
          sort: '',
          icon: ''
        },
        dataRule: {
          typeName: [
            { required: true, message: '输入种类名称不能为空', trigger: 'blur' }
          ],
          showStatus: [
            { required: true, message: '是否显示[0-否,1是]不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ],
          // icon: [
          //   { required: true, message: '图标地址不能为空', trigger: 'blur' }
          // ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.typeId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.typeId) {
            this.$http({
              url: this.$http.adornUrl(`/admin/info/input/type/info/${this.dataForm.typeId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.typeName = data.infoInputType.typeName
                this.dataForm.showStatus = data.infoInputType.showStatus
                this.dataForm.sort = data.infoInputType.sort
                this.dataForm.icon = data.infoInputType.icon
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/admin/info/input/type/${!this.dataForm.typeId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'typeId': this.dataForm.typeId || undefined,
                'typeName': this.dataForm.typeName,
                'showStatus': this.dataForm.showStatus,
                'sort': this.dataForm.sort,
                'icon': this.dataForm.icon
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
